<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\widget\ajax;

use builder\helper\H5;
use builder\widget\BaseWidget;
use yii\helpers\Html;

/**
 * Ajax:多媒体列表
 * @author cleverstone
 * @since ym2.0
 */
class MediaList extends BaseWidget
{
    /**
     * @var array
     */
    public $options = [];

    /**
     * @var string|array 媒体
     */
    public $media;
    public $mediaOption = [];

    /**
     * @var string 媒体标题
     */
    public $mediaHeader;
    public $mediaHeaderOption = [];

    /**
     * @var array 媒体内容
     */
    public $mediaBody = [];
    public $mediaBodyOption = [];

    /**
     * @var array 媒体列表
     */
    public $mediaList = [];
    public $mediaListOption = [];

    /**
     * Initializes the object.
     * This method is called at the end of the constructor.
     * The default implementation will trigger an [[EVENT_INIT]] event.
     */
    public function init()
    {
        parent::init();
        $this->initOption();
    }

    /**
     * @throws \Throwable
     */
    public function run()
    {
        if (
            $this->media
            || $this->mediaHeader
            || $this->mediaBody
            || $this->mediaList
        ) {
            //:1
            echo H5::beginTag('div', ['class' => 'd-flex flex-column lh-1-5']). "\n";
            if (
                $this->media
                || $this->mediaHeader
                || $this->mediaBody
            ) {
                //:2
                echo H5::beginTag('div', $this->options). "\n";
                // Media Image
                echo $this->renderMedia();
                if ($this->mediaHeader || $this->mediaBody) {
                    //Media Header And Media Body
                    echo "\n" . H5::beginTag('div', $this->mediaBodyOption);
                    echo $this->renderMediaHeader();
                    echo $this->renderMediaBody($this->mediaBody);
                    echo "\n" . H5::endTag('div');
                }
                //:2
                echo "\n" . H5::endTag('div');
            }

            if (
                $this->mediaList
                && (
                    $this->media
                    || $this->mediaHeader
                    || $this->mediaBody
                )
            ) {
                //Media Divider
                echo "\n" . Html::tag('div', '', ['class' => 'py-1']);
            }

            if ($this->mediaList) {//Media List
                echo "\n" . H5::beginTag('div', $this->mediaListOption);
                echo $this->renderMediaList($this->mediaList);
                echo "\n" . H5::endTag('div');
            }

            //:1
            echo "\n" . H5::endTag('div');
        } else {
            echo '';
        }
    }

    /**
     * 初始化配置
     */
    protected function initOption()
    {
        H5::addCssClass($this->options, ['widget' => 'media f12']);

        if (!empty($this->media)) {
            $this->media = (array)$this->media;
            H5::addCssClass($this->mediaOption, ['size' => 'w-70 h-70']);
        }

        if ($this->mediaHeader !== null) {
            H5::addCssClass($this->mediaHeaderOption, ['widget' => 'mt-0 f13 layui-font-black']);
        }

        H5::addCssClass($this->mediaBodyOption, ['widget' => 'media-body']);
        H5::addCssClass($this->mediaListOption, ['widget' => 'f12']);
    }

    /**
     * @return string
     * @throws \Throwable
     */
    protected function renderMedia()
    {
        if (empty($this->media)) {
            return null;
        }

        $options['class'] = $this->mediaOption['class'];
        $options['ym2-full-image'] = '';
        H5::addCssStyle($options, ['border-radius' => '4px', 'background-color' => '#fff']);
        foreach ($this->media as &$link) {
            $link = H5::img($link, $options);
        }
        H5::addCssClass($this->mediaOption, ['mr-3']);

        if (count($this->media) == 1) {//One Item
            return Html::tag('div', implode("\n", $this->media), $this->mediaOption);
        } else {//Many Item
            $this->mediaOption['ym2-image-group'] = '';
            $config = [
                'options' => $this->mediaOption,
                'controls' => false,
                'items' => $this->media,
            ];
            Carousel::$autoIdPrefix = 'ML-Carousel';
            return Carousel::widget($config);
        }
    }

    /**
     * @return string
     */
    protected function renderMediaHeader()
    {
        if ($this->mediaHeader !== null) {
            return "\n" . H5::tag('div', $this->mediaHeader, $this->mediaHeaderOption);
        } else {
            return null;
        }
    }

    /**
     * @param array $mediaBody
     * @return string
     */
    protected function renderMediaBody(array $mediaBody)
    {
        if (!empty($mediaBody)) {
            $result = '';
            foreach ($mediaBody as $key => $value) {
                $result .= H5::beginTag('div', ['class' => 'pt-1']);
                if (is_array($value)) {
                    $result .= $this->renderMediaBodyLine($value);
                } else {
                    if (!is_int($key)) {
                        $result .= $this->getInlineBlockDiv($key, ['class' => 'text-muted']);
                    }

                    $result .= $this->getInlineBlockDiv($value, ['class' => 'pr-3']);
                }

                $result .= H5::endTag('div');
            }

            return "\n" . $result;
        } else {
            return null;
        }
    }

    /**
     * @param array $mediaBody
     * @return string
     */
    protected function renderMediaBodyLine(array $mediaBody)
    {
        if (!empty($mediaBody)) {
            $result = '';
            foreach ($mediaBody as $key => $value) {
                if (is_array($value)) {
                    $result .= $this->renderMediaBodyLine($value);
                } else {
                    if (!is_int($key)) {
                        $result .= $this->getInlineBlockDiv($key, ['class' => 'text-muted']);
                    }

                    $result .= $this->getInlineBlockDiv($value, ['class' => 'pr-3']);
                }
            }

            return "\n" . $result;
        } else {
            return null;
        }
    }

    /**
     * @param array $mediaList
     * @return string
     */
    protected function renderMediaList(array $mediaList)
    {
        if (!empty($mediaList)) {
            $result = '';
            foreach ($mediaList as $key => $value) {
                $result .= H5::beginTag('div', ['class' => 'pb-1']);
                if (is_array($value)) {
                    $result .= $this->renderMediaListLine($value);
                } else {
                    if (!is_int($key)) {
                        $result .= $this->getInlineBlockDiv($key, ['class' => 'text-muted']);
                    }

                    $result .= $this->getInlineBlockDiv($value, ['class' => 'pr-3']);
                }

                $result .= H5::endTag('div');
            }

            return "\n" . $result;
        } else {
            return null;
        }
    }

    /**
     * @param array $mediaList
     * @return string
     */
    protected function renderMediaListLine(array $mediaList)
    {
        if (!empty($mediaList)) {
            $result = '';
            foreach ($mediaList as $key => $value) {
                if (is_array($value)) {
                    $result .= $this->renderMediaListLine($value);
                } else {
                    if (!is_int($key)) {
                        $result .= $this->getInlineBlockDiv($key, ['class' => 'text-muted']);
                    }

                    $result .= $this->getInlineBlockDiv($value, ['class' => 'pr-3']);
                }
            }

            return "\n" . $result;
        } else {
            return null;
        }
    }

    /**
     * @param string $content
     * @param array $option
     * @return string
     */
    private function getInlineBlockDiv($content, $option = [])
    {
        H5::addCssClass($option, ['inline-block']);
        return H5::tag('div', $content, $option);
    }
}